<template>
  <div class="wrap" >
    <div class="titles">查询</div>
    <div class="sel">
      <div class="txt">关键词</div>
      <input type="text" placeholder="服务商名称/手机号" v-model="params.keyWord">
      <div class="btn" style="cursor: pointer;" @click="getServerList()">搜索</div>
      <div v-if="roleList.indexOf('增加服务商') != '-1'" class="addpar" @click="addServer()">
        <img class="add" src="../../images/main/add.png" alt> 增加服务商
      </div>
    </div>
    <div class="clear"></div>
<div class="titles">全部</div>
    <el-table :data="serverList" stripe style="width: 100%;">
      <el-table-column align="center" prop="partner_name" label="logo" width="180">
        <template slot-scope="scope">
          <div>
             <img class="tlogo" :src="scope.row.service_logo!='' ? scope.row.service_logo : serverDefaultLogo" alt>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="service_name" label="服务商名称" width="240"></el-table-column>
      <el-table-column align="center" prop="service_type" label="服务类别"></el-table-column>
      <el-table-column align="center" prop="service_account" label="服务商账号"></el-table-column>
      <el-table-column align="center" prop="commission" label="平台佣金"></el-table-column>
      <el-table-column align="center" label="服务区域" prop="region">
      </el-table-column>
      <el-table-column align="center" label="更新时间">
        <template slot-scope="scope">
          <div>{{setDate(scope.row.time) +' '+setTime(scope.row.time)}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="当前状态">
        <template slot-scope="scope">
          <div>{{(scope.row.states == 0) ? '正常' : '冻结中'}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <div>
            <el-popover placement="right" width="200" trigger="click">
              <div class="menu-list">
                <div v-if="roleList.indexOf('分佣设置') != '-1' && roleList.indexOf('服务商管理') != '-1'">
                  <el-button
                    @click="subCommission(scope.row.id)"
                    type="primary"
                    size="mini"
                    round
                  >分佣设置</el-button>
                </div>
                <div v-if="roleList.indexOf('编辑服务商') != '-1'">
                  <el-button
                    @click="editParMan(scope.row.id)"
                    type="primary"
                    size="mini"
                    round
                  >编辑</el-button>
                </div>
                <div v-if="roleList.indexOf('编辑服务商') != '-1'">
                  <el-button @click="resetPwd( scope.row.id )" type="primary" size="mini" round>重置密码</el-button>
                </div>
                <div v-if="roleList.indexOf('服务商管理') != '-1'">
                  <el-button
                    @click="serverDeatil(scope.row.id)"
                    type="primary"
                    size="mini"
                    round
                  >详情</el-button>
                </div>
                <div v-if="roleList.indexOf('收款账号') != '-1'">
                  <el-button @click="reaccServer(scope.row.id)" type="primary" size="mini" round>收款账号</el-button>
                </div>
                <div
                  v-if="roleList.indexOf('冻结/解除冻结') != '-1' && roleList.indexOf('服务商管理') != '-1'"
                >
                  <el-button
                    @click="setFrozen(scope.row.states,scope.row.id)"
                    type="primary"
                    size="mini"
                    round
                  >{{scope.row.states == 0 ? '冻结' : '解除冻结'}}</el-button>
                </div>
              </div>
              <div slot="reference" class="show-menu">···</div>
            </el-popover>
          </div>
        </template>
      </el-table-column>
    </el-table>
        <el-pagination
      :background="true"
      @current-change="changePage"
      layout="total, prev, pager, next, jumper"
      :total="pageTotal"
      style="text-align:center;margin:20px auto;"
    ></el-pagination>
  </div>
</template>
<script>
import Global from '../tool/Global.vue'
import axios from 'axios'
import qs from 'qs' // 将请求转换成form 表单提交形式
import serverDefaultLogo from'@/images/main/server-logo.png'
export default {
  data() {
    return {
      loading: false,
      pageTotal: 1,
      params: {
        page: 1,
        limit: 10,
        keyWord: ""
      },
      serverList:[],
      roleList:'',
      serverDefaultLogo: serverDefaultLogo
    };
  },
  methods: {
    /**
     *  获取权限
     */
    getRole: function () {

      let that = this;
      axios({
          method: 'post',
          url:`${that.$store.state.url}/sysPermission/selectByEmployeeId`,
            data:qs.stringify({
              empId:that.$cookie.get('pid')
            })
      }).then(res => {
            console.log(res)
            let arr = res.data.data
            let role = []
            for(let i = 0; i < arr.length; i++) {
              role.push(arr[i].name)
            }
            this.roleList = role.toString()
        });
    },
    // 分页
    changePage( page ) {
      console.log(page)
      this.params.page = page;
      this.getServerList();
    },
    // 重置密码
    resetPwd( id ) {
      var that = this
      // this.$prompt(
      //   '输入新密码', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消'
      // }).then(({ value }) => {
      //   axios.post (
      //     `${this.$store.state.url}/tService/updatePwd`,
      //     qs.stringify({
      //       id: id,
      //       pwd: value
      //     })
      //   ).then ( response => {
      //     this.$alert ( response.data.msg );
      //   });
      // }).catch(() => {

      // });
      this.$confirm ( '确认重置密码?' ).then (() => {
        axios.post (
          `${this.$store.state.url}/tService/updatePwd`,
          qs.stringify({
            id: id,
          })
        ).then ( response => {
          // this.$alert ( response.data.msg );
          this.$store.state.fn.success( response.data.msg );
          let logData = {
                loginTime:this.$cookie.get("loginTime"),
                account:this.$cookie.get("userAccount"),
                content:'重置服务商密码',
              operationTime:this.setDate(new Date()) + ' ' +this.setTime(new Date())
              }
              axios({
          method: "post",
          url: `${that.$store.state.url}/log/addLog`,
          data: qs.stringify(logData)
        }).then( (res) => {
          console.log(res,'222')
        })
        });
      });
    },
    // 服务商编辑
    editParMan( id ) {
      this.$router.push ({
        path: '/main/editServer',
        query: {
          id: id
        },
        name:'editServer'
      });
    },
    /**
     * 获取服务商列表
     */
    getServerList: function() {
      this.loading = true;
      let that = this;
      axios({
          method: 'post',
          url:`${that.$store.state.url}/tService/getServiceList`,
            data:qs.stringify(that.params)
      }).then(res => {
            console.log(res)
          this.loading = false;
          if (res.data.code == 0) {
            that.serverList = res.data.data
            that.pageTotal = res.data.count
          } else {
            this.$alert(res.msg);
          }
        });
    },
    addServer: function() {
      this.$router.push({
        path: "addServer",
        name: "addServer"
      });
    },
    /**
     * 分佣设置
     */
    subCommission: function(id) {
      Global.serverId = id
      this.$router.push({
        path: "/main/server-subCommission"
      });
    },
    /**
     * 账户信息
     */
    reaccServer: function(id) {
      Global.serverId = id
      console.log(id)
      this.$router.push({
        path: "reaccServer",
        query: {
          id: id
        }
      });
    },
    // 详情
    serverDeatil: function(id) {
      Global.serverId = id
      console.log(id)
      this.$router.push({
        path: "serverDeatils"
      });
    },
    /**
     * 冻结 解冻
     */
    setFrozen: function (states,id) {

      let that = this;
      if (states == 0) {
        states = 1
      } else {
        states = 0
      }
      this.$confirm ( states == 0 ? '是否取消冻结？' : '是否确认冻结？' ).then (() => {
        let data = {
          states:states,
          id:id
        }
        axios({
            method: 'post',
            url:`${that.$store.state.url}/tService/updateStates`,
              data:qs.stringify(data)
        }).then(res => {
          if (res.data.code == 0) {
            this.$store.state.fn.success( states == 0 ? '解除冻结成功' : '确认冻结成功' ).then (() => {
              that.getServerList()
            });
            let logData = {
                loginTime:this.$cookie.get("loginTime"),
                account:this.$cookie.get("userAccount"),
                content:states == 0 ?'解除冻结服务商':'冻结服务商',
              operationTime:this.setDate(new Date()) + ' ' +this.setTime(new Date())
              }
              axios({
          method: "post",
          url: `${that.$store.state.url}/log/addLog`,
          data: qs.stringify(logData)
        }).then( (res) => {
          console.log(res,'222')
        })
          } else {
            this.$alert(res.msg);
          }
        });
      });
    },
     /**
     * 根据时间毫秒数 返回年月日
     */
    setDate: function(str) {
      let times = new Date(str);
      return `${times.getFullYear()}-${
        times.getMonth() + 1 < 10
          ? "0" + (times.getMonth() + 1)
          : times.getMonth() + 1
      }-${times.getDate() < 10 ? "0" + times.getDate() : times.getDate()}`;
    },
    /**
     * 根据时间毫秒数 返回时分秒
     */
    setTime: function(str) {
      let times = new Date(str);
      return `${
        times.getHours() < 10 ? "0" + times.getHours() : times.getHours()
      }:${
        times.getMinutes() < 10 ? "0" + times.getMinutes() : times.getMinutes()
      }:${
        times.getSeconds() < 10 ? "0" + times.getSeconds() : times.getSeconds()
      }`;
    }
  },
  mounted () {
    this.getServerList()
    this.getRole()
  }
};
</script>
<style scoped>

.titles {
  height: 62px;
  line-height: 62px;
  color: #778ca2ff;
  font-size: 14px;
  width: 100%;
  display: inline-block;
}
.wrap {
  /* padding: 68px 136px; */
  /* width: 100%; */
  width: 90%;
  margin: 0 auto;
}
.tlogo {
  max-width: 100%;
  max-height: 100px;
}
.sel {
  height: 32px;
  line-height: 32px;
  padding: 23px 20px;
  background: #fff;
}
.sel .txt {
  float: left;
  font-family: PingFangSC-Medium;
  font-size: 14px;
  color: rgba(37, 38, 49, 1);
  font-weight: bold;
  width: 42px;
}
.sel input {
  float: left;
  width: 232px;
  height: 32px;
  background: rgba(255, 255, 255, 1);
  border-radius: 16px;
  border: 1px solid rgba(232, 236, 239, 1);
  color: #888888;
  margin-left: 17px;
  text-align: center;
  box-sizing: border-box;
  outline: none;
}
.btn {
  float: left;
  color: #fff;
  width: 80px;
  height: 32px;
  background: rgba(75, 116, 255, 1);
  border-radius: 24px;
  border: 1px solid rgba(75, 116, 255, 1);
  text-align: center;
  margin-left: 37px;
}

.addpar {
  float: right;
  font-size: 14px;
  color: #fff;
  cursor: pointer;
  width: 114px;
  height: 32px;
  background: rgba(75, 116, 255, 1);
  border-radius: 24px;
  border: 1px solid rgba(75, 116, 255, 1);
}
.add {
  width: 18px;
  height: 18px;
  float: left;
  margin: 8px;
}
.show-menu {
  color: rgba(75, 116, 255, 1);
  cursor: pointer;
}
.menu-list div {
  margin: 5px;
  float: left;
  box-sizing: border-box;
  width: 40%;
}
</style>
<style>
.el-pagination.is-background .el-pager li:not(.disabled):hover{
    background-color:#085dab;
    color: #d2e0ef;
}
.el-pagination.is-background .el-pager li:not(.disabled).active{
  background-color:#085dab;
}
</style>

